import CanvasLayer from "./core/canvas-layer.js";
import Input from "./core/input.js";
import { PauseMode } from "./core/node.js";

const BASE_X = 160;
const BASE_Y = 28;

export class DebugInfoLayer extends CanvasLayer {
  constructor(game, viewport) {
    super(viewport, 2, 'debug-info');
    this.setPauseMode(PauseMode.Process);
    this.game = game;
  }

  update(delta) {
    const { context2d } = this;
    context2d.clearRect(0, 0, this.canvas.width, this.canvas.height);

    let y = BASE_Y;
    let newLine = () => y += 20;
    const { game } = this;
    const { camera, player } = game;

    context2d.font = '14px monospace';
    if (game.background.theme == 'forest') {
      context2d.fillStyle = 'rgba(255, 255, 255, 0.6)';
    }
    context2d.fillText('camera.position: ' + camera.position, BASE_X, BASE_Y);
    context2d.fillText('camera.offset: ' + camera.offset, BASE_X, newLine());
    context2d.fillText('camera.smoothingEnabled: ' + camera.smoothingEnabled, BASE_X, newLine());
    context2d.fillText('camera.smoothingSpeed:   ' + camera.smoothingSpeed, BASE_X, newLine());
    context2d.fillText('camera.followTarget:     '
      + (camera.followTarget ? camera.followTarget.constructor.name : null), BASE_X, newLine());
    context2d.fillText('camera.lookAtTarget:     '
      + (camera.lookAtTarget ? camera.lookAtTarget.constructor.name : null), BASE_X, newLine());
    context2d.fillText('node count:   ' + game.nodes.length, BASE_X, newLine());
    context2d.fillText('enemy count:    ' + game.enemyManager.count(), BASE_X, newLine());
    context2d.fillText('particle count: ' + game.particleGroup.count(), BASE_X, newLine());
    context2d.fillText('player.velocity:  ' + player.velocity, BASE_X, newLine());
    context2d.fillText('player.position:  ' + player.position, BASE_X, newLine());
    context2d.fillText('player.state:     ' + player.nowState.id, BASE_X, newLine());
  
    y = BASE_Y;
    const fps = Math.round(1 / delta);
    context2d.fillText('FPS:   ' + fps, game.width - 128, BASE_Y);
    context2d.fillText('Delta: ' + delta.toFixed(6), game.width - 128, newLine());
    context2d.fillText('Input.pressedKeys:', game.width - 158, newLine());
    Input.getPressedKeys().forEach((key) => {
      context2d.fillText(key, game.width - 70, newLine());
    });
  }
}

export function parseSearchParams() {
  if (!location.search) {
    return {};
  }
  const pairs = location.search.substring(1).split('&');
  return pairs.reduce((obj, pair) => {
    const [ k, v ] = pair.split('=');
    obj[k] = v;
    return obj;
  }, {});
}